home *** CD-ROM | disk | FTP | other *** search
/ Chip 2005 June / ccd0605.iso / Software / Freeware / Programare / highlight / highlight-W32GUI-2.2-10b-Setup.exe / {app} / src / xmlgenerator.cpp < prev    next >
C/C++ Source or Header  |  2005-03-31  |  6KB  |  206 lines

  1. /***************************************************************************
  2.                           xmlcode.cpp  -  description
  3.                              -------------------
  4.     begin                : Do 20.01.2005
  5.     copyright            : (C) 2005 by Andre Simon
  6.     email                : andre.simon1@gmx.de
  7.  ***************************************************************************/
  8.  
  9. /***************************************************************************
  10.  *                                                                         *
  11.  *   This program is free software; you can redistribute it and/or modify  *
  12.  *   it under the terms of the GNU General Public License as published by  *
  13.  *   the Free Software Foundation; either version 2 of the License, or     *
  14.  *   (at your option) any later version.                                   *
  15.  *                                                                         *
  16.  ***************************************************************************/
  17.  
  18. #include "xmlgenerator.h"
  19.  
  20. using namespace std;
  21. namespace highlight {
  22.  
  23.     XmlGenerator::XmlGenerator(const string &colourTheme,const string &enc, bool omitEnc)
  24.     : CodeGenerator(colourTheme),
  25.     encoding(enc), omitEncoding(omitEnc)
  26. {
  27.     styleTagOpen.push_back(getOpenTag("def"));
  28.     styleTagOpen.push_back(getOpenTag("str"));
  29.     styleTagOpen.push_back(getOpenTag("num"));
  30.     styleTagOpen.push_back(getOpenTag("slc"));
  31.     styleTagOpen.push_back(getOpenTag("com"));
  32.     styleTagOpen.push_back(getOpenTag("esc"));
  33.     styleTagOpen.push_back(getOpenTag("dir"));
  34.     styleTagOpen.push_back(getOpenTag("dstr"));
  35.     styleTagOpen.push_back(getOpenTag("line"));
  36.     styleTagOpen.push_back(getOpenTag("sym"));
  37.  
  38.     styleTagClose.push_back(getCloseTag("def"));
  39.     styleTagClose.push_back(getCloseTag("str"));
  40.     styleTagClose.push_back(getCloseTag("num"));
  41.     styleTagClose.push_back(getCloseTag("slc"));
  42.     styleTagClose.push_back(getCloseTag("com"));
  43.     styleTagClose.push_back(getCloseTag("esc"));
  44.     styleTagClose.push_back(getCloseTag("dir"));
  45.     styleTagClose.push_back(getCloseTag("dstr"));
  46.     styleTagClose.push_back(getCloseTag("line"));
  47.     styleTagClose.push_back(getCloseTag("sym"));
  48.  
  49.     spacer = " ";
  50.     newLineTag = "<br />\n";
  51. }
  52.  
  53. string XmlGenerator::getStyleDefinition()
  54. {
  55.     if (styleDefinitionCache.empty()) {
  56.         ostringstream os;
  57.         os << "\n<style>\n"
  58.            << "\t<bgcolor value=\""
  59.            << (docStyle.getBgColour().getHexRedValue())
  60.            << (docStyle.getBgColour().getHexGreenValue())
  61.            << (docStyle.getBgColour().getHexBlueValue())
  62.            << "\" />\n"
  63.            << "\t<font size=\""
  64.            << docStyle.getFontSize()
  65.            << "\" family=\"Courier\" />\n";
  66.  
  67.         os << formatStyleAttributes("def", docStyle.getDefaultStyle())
  68.            << formatStyleAttributes("num", docStyle.getNumberStyle())
  69.            << formatStyleAttributes("esc", docStyle.getEscapeCharStyle())
  70.            << formatStyleAttributes("str", docStyle.getStringStyle())
  71.            << formatStyleAttributes("dstr", docStyle.getDirectiveStringStyle())
  72.            << formatStyleAttributes("slc", docStyle.getSingleLineCommentStyle())
  73.            << formatStyleAttributes("com", docStyle.getCommentStyle())
  74.            << formatStyleAttributes("dir", docStyle.getDirectiveStyle())
  75.            << formatStyleAttributes("sym", docStyle.getSymbolStyle())
  76.            << formatStyleAttributes("line", docStyle.getLineStyle());
  77.  
  78.         KeywordStyles styles = docStyle.getKeywordStyles();
  79.         for (KSIterator it=styles.begin(); it!=styles.end(); it++){
  80.             os << formatStyleAttributes(it->first, *(it->second));
  81.         }
  82.         os << "</style>\n";
  83.         styleDefinitionCache=os.str();
  84.     }
  85.     return styleDefinitionCache;
  86. }
  87.  
  88.  
  89. string XmlGenerator::formatStyleAttributes(const string & elemName,
  90.                                            const ElementStyle & elem)
  91. {
  92.     ostringstream s;
  93.     s  << "\t<class name=\""
  94.        << elemName
  95.        <<"\" color=\""
  96.        << (elem.getColour().getHexRedValue())
  97.        << (elem.getColour().getHexGreenValue())
  98.        << (elem.getColour().getHexBlueValue()  )
  99.        << "\" bold=\""
  100.        << ( elem.isBold() ? "true" :"false" )
  101.        << "\" italic=\""
  102.        << ( elem.isItalic() ? "true" :"false" )
  103.        << "\" underline=\""
  104.        << ( elem.isUnderline() ? "true" :"false" )
  105.        << "\" />\n" ;
  106.     return  s.str();
  107. }
  108.  
  109.  
  110. XmlGenerator::XmlGenerator()
  111. {}
  112. XmlGenerator::~XmlGenerator()
  113. {}
  114.  
  115. string  XmlGenerator::getOpenTag(const string& styleName ){
  116.     return "<"+styleName+">";
  117. }
  118.  
  119. string  XmlGenerator::getCloseTag(const string& styleName ){
  120.     return "</"+styleName+">";
  121. }
  122.  
  123. string XmlGenerator::getHeader(const string & title)
  124. {
  125.     ostringstream header;
  126.     header << "<?xml version=\"1.0\"";
  127.     if (!omitEncoding) {
  128.         header << " encoding=\"" << encoding << "\"";
  129.     }
  130.     header << "?>\n<document>" << getStyleDefinition();
  131.     return header.str();
  132. }
  133.  
  134. void XmlGenerator::printBody()
  135. {
  136.   *out << "<source>\n";
  137.   processRootState();
  138.   *out << "</source>\n";
  139. }
  140.  
  141.  
  142. string XmlGenerator::getFooter()
  143. {
  144.   ostringstream os;
  145.   os <<"</document>\n";
  146.   os<< "<!-- XML generated by Highlight "
  147.      << HIGHLIGHT_VERSION
  148.      << ", "
  149.      << HIGHLIGHT_URL
  150.      <<" -->\n";
  151.   return os.str();
  152. }
  153.  
  154. string XmlGenerator::maskCharacter(unsigned  char c)
  155. {
  156.   switch (c)
  157.     {
  158.       case '<' :
  159.       return "<";
  160.       break;
  161.     case '>' :
  162.       return ">";
  163.       break;
  164.     case '&' :
  165.       return "&";
  166.       break;
  167.     case '\"' :
  168.       return """;
  169.       break;
  170.  
  171. // skip  first byte of multibyte chracters
  172.   /*  #ifndef _WIN32
  173.     case 195:
  174.       return string("");
  175.       break;
  176. #endif*/
  177.  
  178.     default:
  179.       string m;
  180.       m += c;
  181.       return m;
  182.     }
  183. }
  184.  
  185. /*string XmlGenerator::getNewLine(){
  186.   string nlStr;
  187.   if (currentState!=_UNKNOWN){
  188.       nlStr+=styleTagClose[getStyleID(currentState, currentKeywordClass)];
  189.   }
  190.   nlStr += newLineTag;
  191.   if (currentState!=_UNKNOWN){
  192.       nlStr+=styleTagOpen[getStyleID(currentState, currentKeywordClass)];
  193.   }
  194.   return nlStr;
  195. }
  196. */
  197. string XmlGenerator::getMatchingOpenTag(unsigned int styleID){
  198.     return getOpenTag(langInfo.getKeywordClasses()[styleID]);
  199. }
  200.  
  201. string XmlGenerator::getMatchingCloseTag(unsigned int styleID){
  202.     return getCloseTag(langInfo.getKeywordClasses()[styleID]);
  203. }
  204.  
  205. }
  206.